package br.com.aquinoevasconcelos.servlets.agendamento;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.aquinoevasconcelos.excecao.ValidacaoException;
import br.com.aquinoevasconcelos.fachada.Fachada;
import br.com.aquinoevasconcelos.modelos.Agendamento;
import br.com.aquinoevasconcelos.user.UsuarioCliente;

/**
 * Servlet implementation class InserirAgendamento
 */
@WebServlet("/InserirAgendamento")
public class InserirAgendamento extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
    public InserirAgendamento() {
        super();

    }
    
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
		Fachada fachada = Fachada.getInstancia();
		Agendamento agendamento = new Agendamento();
		UsuarioCliente cliente = new UsuarioCliente();
		
		//cliente.setId(Long.parseLong(request.getParameter("id")));
		
		System.out.println(request.getParameter("hora"));
		agendamento.setNomeUsuario(request.getParameter("nome"));
		agendamento.setTitulo(request.getParameter("titulo"));
		agendamento.setContador(request.getParameter("contador"));
		agendamento.setEmail(request.getParameter("email"));
		agendamento.setData(request.getParameter("data"));
		agendamento.setHora(request.getParameter("hora"));
		//agendamento.getUsuariocleinte().add(cliente);
		
		try {
			fachada.inserirAgendamento(agendamento);
			
			response.sendRedirect("agendamento_consultas.jsp");
			
		}catch(ValidacaoException e){
			
			response.sendRedirect("./agendamento_consultas.jsp?mensagem=erro");
		}
		catch (Exception e) {
			
			RequestDispatcher rd = request.getRequestDispatcher("/pagina_erro/erro.jsp");
			rd.forward(request, response);
			e.printStackTrace();

		}
    	
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

}
